// pages/gooddetail/gooddetail.js
Page({
  data: {
    current: 0,
    autoplay: false,
    duration: 500,
    interval: 3000,
    goodDetail: {},
    swiperList: [],
    goodServiceList: [],
    goodCommentTotal: 0,
    containerShow: false,
    containerStatus: '',
    isLogin: false,
    isLoginShow: false,
    addCartTotal: 1
  },
  onLoad(query) {
    this.getGoodDetail(query.goods_id)
    this.getGoodService(query.goods_id)
    this.getGoodComment(query.goods_id)
  },
  onShow() {
    this.onRefreshPage()
  },
  // 页面刷新
  onRefreshPage() {
    this.setData({
      isLogin: getApp().globalData.isLogin
    })
  },
  // 获取商品详情
  async getGoodDetail(goodsId) {
    const res = await wx.http.get('/goods/detail', {goodsId})
    const goodDetail = res.data.data.detail
    const swiperList = goodDetail.goods_images.map(item => item.preview_url)
    this.setData({goodDetail, swiperList})
  },
  // 获取商品保障服务
  async getGoodService(goodsId) {
    const res = await wx.http.get('/goods.service/list', {goodsId})
    const goodServiceList = res.data.data.list
    this.setData({goodServiceList})
  },
  // 获取详情页商品评价
  async getGoodComment(goodsId) {
    const res = await wx.http.get('/comment/listRows', {goodsId, limit: 2})
    const goodCommentList = res.data.data.list
    const goodCommentTotal = res.data.data.total
    this.setData({goodCommentList, goodCommentTotal})
  },
  // 点击开启商品保障服务弹窗
  showServiceContainer() {
    this.setData({
      containerShow: true,
      containerStatus: 'service'
    })
  },
  // 点击关闭弹窗
  closeContainer() {
    this.setData({
      containerShow: false,
      containerStatus: ''
    })
  },
  // 点击进入首页
  toHome() {
    wx.switchTab({
      url: '../index/index'
    })
  },
  toCart() {
    wx.switchTab({
      url: '../cart/cart'
    })
  },
  // 点击进入商品评价
  toComment(ev) {
    wx.navigateTo({
      url: `../goodcomment/goodcomment?goodsId=${ev.mark.goodsId}`,
    })
  },
  // 未登录弹框
  loginCancel() {
    this.setData({isLoginShow: false})
  },
  loginConfirm() {
    this.setData({isLoginShow: false})
    wx.navigateTo({
      url: '../../user_pkg/pages/login/login'
    })
  },
  // 加入购物车弹窗
  addCartBox() {
    if(this.data.isLogin === false) {
      this.setData({isLoginShow: true})
    } else {
      this.setData({
        containerStatus: 'addCart',
        containerShow: true
      })
    }
  },
  // 立即购买弹窗
  buyBox() {
    if(this.data.isLogin === false) {
      this.setData({isLoginShow: true})
    } else {
      this.setData({
        containerStatus: 'buy',
        containerShow: true
      })
    }
  },
  // 监测加购数量
  addCartTotalInput(ev) {
    this.setData({
      addCartTotal: ev.detail.value
    })
  },
  cartTotalAdd() {
    let addCartTotal = this.data.addCartTotal
    addCartTotal++
    this.setData({addCartTotal})
  },
  cartTotalReduce() {
    let addCartTotal = this.data.addCartTotal
    addCartTotal--
    this.setData({addCartTotal})
  },
  // 加入购物车
  async addCart() {
    const goodsSkuId = this.data.goodDetail.skuList[0].goods_sku_id
    const res = await wx.http.post('/cart/add', {
      goodsId: this.data.goodDetail.goods_id,
      goodsNum: this.data.addCartTotal,
      goodsSkuId
    })
    if(res.data.status === 200) {
      wx.showToast({
        title: '加入购物车成功',
        duration: 2000
      })
      this.closeContainer()
    } else {
      wx.showToast({
        title: '加入失败',
        icon: 'error',
        duration: 2000
      })
    }
  },
  // 立即购买
  buyNow(ev) {
    const mode = ev.mark.mode
    const goodsId = ev.mark.goodsId
    const goodsNum = ev.mark.goodsNum
    const goodsSkuId = ev.mark.goodsSkuId
    wx.navigateTo({
      url: `../../order_pkg/settlement/settlement?mode=${mode}&goodsId=${goodsId}&goodsNum=${goodsNum}&goodsSkuId=${goodsSkuId}`
    })
    console.log('按钮被点击了')
  }
})